package com.lj.luoye.lotteryticket.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lj.luoye.lotteryticket.entity.Unionlotto;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class LotteryBuyMultiDao {
    public final static String table_name = "lottery_buy_multi";
    public DBHelper dbHelper;
    public Context context;
    String[] fields = new String[]{"id","date","description", "redBalls", "blueBalls","bought","luckResult"};

    public LotteryBuyMultiDao(Context context) {
        this.dbHelper = new DBHelper(context);
        this.context = context;
    }

    public void saveUnionlottos(String date,List<Unionlotto> unionlottoList){
        //先查询，如果有了的话就先删除
        List<Unionlotto> unionlottoListGet = getByDate(date);
        if(unionlottoListGet.size()>0){
            deleteByDate(date);
        }
        for(Unionlotto unionlotto:unionlottoList){
            unionlotto.date=date;
            insert(unionlotto);
        }
    }

    public long updateUnionlottoToBought(String date){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("bought",1);
        long res = db.update(LotteryBuyMultiDao.table_name,contentValues, "date=?", new String[]{date});
        db.close();
        return res;
    }

    public long deleteByDate(String date){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        long res = db.delete(LotteryBuyMultiDao.table_name, "date=?", new String[]{date});
        db.close();
        return res;
    }

    public List<String> getDates(){
        List<String> dates=new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(LotteryBuyMultiDao.table_name,fields,null,null,"date",null,null);
        while(cursor.moveToNext()){
            String date = cursor.getString(cursor.getColumnIndex("date"));
            dates.add(date);
        }
        db.close();
        return dates;
    }

    public List<String> getMonths(){
        List<String> dates=new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(LotteryBuyMultiDao.table_name,fields,null,null,"date",null,null);
        while(cursor.moveToNext()){
            String date = cursor.getString(cursor.getColumnIndex("date"));
            dates.add(date);
        }
        db.close();
        Set<String> months=new HashSet<>();
        for(String date:dates){
            String month=date.substring(0,7);
            months.add(month);
        }
        List<String> monthList=new ArrayList<>();
        monthList.addAll(months);
        return monthList;
    }

    public List<Unionlotto> getByDate(String date){
        List<Unionlotto> unionlottoList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(LotteryBuyMultiDao.table_name,fields,"date=?",new String[]{date},null,null,null);
        while(cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String description = cursor.getString(cursor.getColumnIndex("description"));
            String redBalls = cursor.getString(cursor.getColumnIndex("redBalls"));
            String blueBalls = cursor.getString(cursor.getColumnIndex("blueBalls"));
            int bought = cursor.getInt(cursor.getColumnIndex("bought"));
            String luckResult = cursor.getString(cursor.getColumnIndex("luckResult"));

            String[] redBallArray = redBalls.split(",");
            List<String> redBallList = new ArrayList<>(Arrays.asList(redBallArray));
            String[] blueBallArray = blueBalls.split(",");
            List<String> blueBallList = new ArrayList<>(Arrays.asList(blueBallArray));
            Unionlotto unionlotto = new Unionlotto(id,date,description,redBallList,blueBallList,bought,luckResult);
            unionlottoList.add(unionlotto);
        }
        db.close();
        return unionlottoList;
    }

    public int insert(Unionlotto unionlotto){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("date",unionlotto.date);
        contentValues.put("description",unionlotto.description);
        int i=0;
        StringBuilder redBalls= new StringBuilder();
        for(String redBall:unionlotto.getRedBalls()){
            if(i==0) {
                redBalls.append(redBall);
            }else{
                redBalls.append(",").append(redBall);
            }
            i++;
        }
        contentValues.put("redBalls", redBalls.toString());

        i=0;
        StringBuilder blueBalls= new StringBuilder();
        for(String blueBall:unionlotto.blueBalls){
            if(i==0) {
                blueBalls.append(blueBall);
            }else{
                blueBalls.append(",").append(blueBall);
            }
            i++;
        }
        contentValues.put("blueBalls", blueBalls.toString());
        contentValues.put("bought",unionlotto.bought);
        contentValues.put("luckResult",unionlotto.luckResult);
        long id = db.insert(LotteryBuyMultiDao.table_name, null, contentValues);
        db.close();
        return (int) id;
    }

    public int updateLuckResult(int id,String luckResult){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("luckResult",luckResult);
        int res = db.update(LotteryBuyMultiDao.table_name, contentValues, "id=?", new String[]{id+""});
        db.close();
        return res;
    }

    public List<Unionlotto> getBoughtByMonth(String month){
        List<Unionlotto> unionlottoList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(LotteryBuyMultiDao.table_name,fields,"bought=1 and date like '"+month+"%'",new String[]{},null,null,null);
        while(cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String date = cursor.getString(cursor.getColumnIndex("date"));
            String description = cursor.getString(cursor.getColumnIndex("description"));
            String redBalls = cursor.getString(cursor.getColumnIndex("redBalls"));
            String blueBalls = cursor.getString(cursor.getColumnIndex("blueBalls"));
            int bought = cursor.getInt(cursor.getColumnIndex("bought"));
            String luckResult = cursor.getString(cursor.getColumnIndex("luckResult"));

            String[] redBallArray = redBalls.split(",");
            List<String> redBallList = new ArrayList<>(Arrays.asList(redBallArray));
            String[] blueBallArray = blueBalls.split(",");
            List<String> blueBallList = new ArrayList<>(Arrays.asList(blueBallArray));
            Unionlotto unionlotto = new Unionlotto(id,date,description,redBallList,blueBallList,bought,luckResult);
            unionlottoList.add(unionlotto);
        }
        db.close();
        return unionlottoList;
    }

    public List<Unionlotto> getAll(){
        List<Unionlotto> unionlottoList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(LotteryBuyMultiDao.table_name,fields,"",new String[]{},null,null,null);
        while(cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String date = cursor.getString(cursor.getColumnIndex("date"));
            String description = cursor.getString(cursor.getColumnIndex("description"));
            String redBalls = cursor.getString(cursor.getColumnIndex("redBalls"));
            String blueBalls = cursor.getString(cursor.getColumnIndex("blueBalls"));
            int bought = cursor.getInt(cursor.getColumnIndex("bought"));
            String luckResult = cursor.getString(cursor.getColumnIndex("luckResult"));

            String[] redBallArray = redBalls.split(",");
            List<String> redBallList = new ArrayList<>(Arrays.asList(redBallArray));
            String[] blueBallArray = blueBalls.split(",");
            List<String> blueBallList = new ArrayList<>(Arrays.asList(blueBallArray));
            Unionlotto unionlotto = new Unionlotto(id,date,description,redBallList,blueBallList,bought,luckResult);
            unionlottoList.add(unionlotto);
        }
        db.close();
        return unionlottoList;
    }

    public int importRow(Unionlotto unionlotto){
        int i=0;
        StringBuilder redBalls= new StringBuilder();
        for(String redBall:unionlotto.getRedBalls()){
            if(i==0) {
                redBalls.append(redBall);
            }else{
                redBalls.append(",").append(redBall);
            }
            i++;
        }
        i=0;
        StringBuilder blueBalls= new StringBuilder();
        for(String blueBall:unionlotto.blueBalls){
            if(i==0) {
                blueBalls.append(blueBall);
            }else{
                blueBalls.append(",").append(blueBall);
            }
            i++;
        }


        //先查询，是否已经存在
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(LotteryBuyMultiDao.table_name,fields,"date=? and redBalls=? and blueBalls=? ",
                new String[]{unionlotto.date,redBalls.toString(),blueBalls.toString()},null,null,null);
//        Cursor cursor = db.query(LotteryBuyDao.table_name,fields,"date=? ",
//                new String[]{unionlotto.date},null,null,null);
        int count=cursor.getCount();
        db.close();
        System.out.println("count->"+count);
        if(count>0){
            return 0;
        }
        return insert(unionlotto);
    }


}
